import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/config/address.dart';
import 'package:tiktokadult/hj_page/mine_business/make_money/alipay_ccdcapi_model.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';
import 'package:tiktokadult/hj_utils/global.dart';
import 'package:tiktokadult/hj_utils/http_manager.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';
import 'package:tiktokadult/tools_base/loading/LoadingHelper.dart';
import 'package:tiktokadult/tools_base/widget/common_dialog.dart';

class AddBankDialog extends StatefulWidget {
  const AddBankDialog({super.key});

  @override
  State<AddBankDialog> createState() => _AddBankDialogState();
}

class _AddBankDialogState extends State<AddBankDialog> {
  late final bankNameCtr = TextEditingController();
  late final bankNoCtr = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return CommonDialog(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            '添加银行卡',
            style: TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.w500),
          ),
          12.sizeBoxH,
          Divider(height: .5, color: Colors.white.withOpacity(.1)),
          12.sizeBoxH,
          Container(
            height: 44,
            decoration: BoxDecoration(
              color: Colors.white.withOpacity(.1),
              borderRadius: BorderRadius.circular(6),
            ),
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: TextField(
              maxLength: 20,
              controller: bankNameCtr,
              style: TextStyle(color: Colors.white),
              inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[a-zA-Z\u4E00-\u9F5a]'))],
              decoration: InputDecoration(
                border: InputBorder.none,
                counterText: '',
                contentPadding: EdgeInsets.zero,
                isDense: true,
                hintText: '请输入姓名',
                hintStyle: TextStyle(
                  fontSize: 14,
                  color: Colors.white.withOpacity(.45),
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
          ),
          12.sizeBoxH,
          Container(
            height: 44,
            decoration: BoxDecoration(
              color: Colors.white.withOpacity(.1),
              borderRadius: BorderRadius.circular(6),
            ),
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.symmetric(horizontal: 16),
            child: TextField(
              maxLength: 20,
              controller: bankNoCtr,
              style: TextStyle(color: Colors.white, fontWeight: FontWeight.w400),
              inputFormatters: [FilteringTextInputFormatter.allow(RegExp(r'[0-9]'))],
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: '请输入银行卡号',
                counterText: '',
                contentPadding: EdgeInsets.zero,
                isDense: true,
                hintStyle: TextStyle(
                  fontSize: 14,
                  color: Colors.white.withOpacity(.45),
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
          ),
          24.sizeBoxH,
          InkWell(
            onTap: () {
              if (bankNoCtr.text.isEmpty) {
                showToast('请输入银行卡号');
                return;
              }
              if (bankNameCtr.text.isEmpty) {
                showToast('请输入持卡人姓名');
                return;
              }
              _bankNumberVerifyReq();
            },
            child: Container(
              height: 44,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: AppColors.actionRed,
                borderRadius: BorderRadius.circular(3),
              ),
              child: Text(
                "立即提交",
                style: TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500),
              ),
            ),
          )
        ],
      ),
    );
  }

  _buildItem(String title) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 32),
      child: Row(
        children: [
          Container(
            width: 5,
            height: 9,
            decoration: BoxDecoration(color: AppColors.primaryHighColor, borderRadius: BorderRadius.circular(6)),
          ),
          6.5.sizeBoxW,
          Text(
            title,
            style: textStyle(14, Color(0xff666666), FontWeight.w400),
          ),
        ],
      ),
    );
  }

  ///校验银行卡信息
  void _bankNumberVerifyReq() async {
    String bankNum = bankNoCtr.text.trim();
    if (bankNum.isEmpty || bankNum.length < 13) {
      showToast("银行卡号错误");
      return;
    }
    LoadingHelper.showLoading();
    httpManager.fetchResponseByGET(Address.ALI_CCD_API + bankNum).then((ret) {
      LoadingHelper.dismissLoading();
      if (!ret.isSuccess) {
        showToast("银行卡验证失败.");
        return;
      }
      if (ret.isSuccess) {
        final model = ApcApiModel.fromMap(Map<String, dynamic>.from(ret.data));
        if (model?.validated ?? false) {
          // model.bank, model.cardType
          debugLog("model.bank-->", "${model?.bank}");
          debugLog("model.cardType-->", "${model?.cardType}");
          // _commonWithdrawReq(true, bank: model?.bank);
          _addBankCard(model!);
        } else {
          showToast("银行卡验证失败");
        }
      } else {
        showToast("银行卡验证失败");
      }
    });
  }

  _addBankCard(ApcApiModel model) async {
    final res = await MineService.getAddBankCard(model.key, bankNameCtr.text, model.bank, model.cardType);
    if (res) {
      Get.back(result: true);
    }
  }
}
